package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GdlzInfoActivity extends BaseActivity implements PullDownView.OnPullDownListener, AdapterView.OnItemClickListener  {
    public static final String TAG = "GdlzInfoActivity";
    private TextView mTextViewManno;
    private Button mButton;
    private RelativeLayout mRelativeLayout;
    private MannoLzAdapter adapter;
    private ListView gdlzinfo_lv;
    private PullDownView mPullDownView;
    private List<GdlzInfoBean> mGdlzInfoBeans;
    private String manno ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlz_info);
        //initData();
        initView();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if (data != null) {
                result = data.getStringExtra("result");
            }
            mTextViewManno.setText(String.valueOf(result));
            manno = String.valueOf(result);
            initData();
        }
    }
    private void initData() {
        try {
        Map<String, String> params = new HashMap<String, String>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Manno",manno);
        params.put("action", "203");
        params.put("params", jsonObject.toString());
        VolleyUtil.sendStringRequestByGet(GdlzInfoActivity.this, HttpConfig.HTTP_SFC_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
            @Override
            public void onResponse(String result) {
                super.onResponse(result);
                if (result == null|| result.equals("0")) {
                    AppUtils.showTip(GdlzInfoActivity.this, "该工单无流转信息");
                    return;
                }
                mGdlzInfoBeans = new Gson().fromJson(result, new TypeToken<List<GdlzInfoBean>>() {
                }.getType());

                //adapter = new MannoLzAdapter(GdlzInfoActivity.this);
                gdlzinfo_lv.setAdapter(adapter);
            }

            @Override
            public void onCodeError(int code, String msg) {
                AppUtils.showTip(GdlzInfoActivity.this, msg);
            }

            @Override
            public void onVolleyError(VolleyError volleyError) {
                AppUtils.showTip(GdlzInfoActivity.this, R.string.network_error);
            }

            @Override
            public void onException(Exception e) {
                AppUtils.showTip(GdlzInfoActivity.this, R.string.network_error);
            }
        });
        } catch (Exception e) {
            e.printStackTrace();
            AppUtils.showTip(GdlzInfoActivity.this, "查询失败，请联系IT人员");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView(){
        titleBar();

        mTextViewManno = (TextView) findViewById(R.id.top_search);
        /*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
        mPullDownView = (PullDownView) findViewById(R.id.kjinfo_lzinfo);

        mPullDownView.setOnPullDownListener(this);

        gdlzinfo_lv = mPullDownView.getListView();

        gdlzinfo_lv.setOnItemClickListener(this);

        adapter = new MannoLzAdapter(GdlzInfoActivity.this);
        //mannoinfo_lv.setAdapter(adapter);

        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        // 隐藏 并禁用尾部
        mPullDownView.setHideFooter();
    }

    private void titleBar() {
        //控制登录用户名图标大小
        EditText editText1 = (EditText) findViewById(R.id.top_search);
        Drawable drawable1 = getResources().getDrawable(R.drawable.search_icon2);
        drawable1.setBounds(0, 0, 40, 40);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        editText1.setCompoundDrawables(drawable1, null, null, null);//只放左边
        editText1.setInputType(InputType.TYPE_NULL);
        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlzInfoActivity.this, CaptureActivity.class), 0x0002);
            }
        });

        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        mButton = (Button)findViewById(R.id.top_title_refres_with_icon);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlzInfoActivity.this, CaptureActivity.class), 0x0002);
            }
        });
    }

    /**
     * listview单项点击事件
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Toast.LENGTH_SHORT).show();
    }

    /**
     * listview刷新
     */
    public void onRefresh() {

    }

    /**
     * listview更多
     */
    public void onMore() {
        return;
    }

    public class MannoLzAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;
        public MannoLzAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mGdlzInfoBeans.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.sfc_gdlz_info_list, null);
            holder.sfc_mannolz_list_prosscode = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_prosscode);
            holder.sfc_mannolz_list_prossname = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_prossname);
            holder.sfc_mannolz_list_manqty = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_manqty);
            holder.sfc_mannolz_list_issqty = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_issqty);
            holder.sfc_mannolz_list_areaname = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_areaname);
            holder.sfc_mannolz_list_nextpross = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_nextpross);
            holder.sfc_mannolz_list_nextprossname = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_nextprossname);
            holder.sfc_mannolz_list_date = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_date);
            holder.sfc_mannolz_list_user = (TextView) convertView.findViewById(R.id.sfc_gdlzinfo_list_user);
            holder.sfc_mannolz_list_prosscode.setText(mGdlzInfoBeans.get(position).getProssCode());
            holder.sfc_mannolz_list_prossname.setText(mGdlzInfoBeans.get(position).getProssName());
            holder.sfc_mannolz_list_manqty.setText(String.valueOf(mGdlzInfoBeans.get(position).getManqty()));
            holder.sfc_mannolz_list_issqty.setText(String.valueOf(mGdlzInfoBeans.get(position).getIssqty()));
            holder.sfc_mannolz_list_areaname.setText(mGdlzInfoBeans.get(position).getAreaName());
            holder.sfc_mannolz_list_nextpross.setText(mGdlzInfoBeans.get(position).getNextPross());
            holder.sfc_mannolz_list_nextprossname.setText(mGdlzInfoBeans.get(position).getNextProssName());
            holder.sfc_mannolz_list_date.setText(mGdlzInfoBeans.get(position).getCreateTime().toString());
            holder.sfc_mannolz_list_user.setText(mGdlzInfoBeans.get(position).getUserName().toString());
            convertView.setTag(holder);
            return convertView;
        }
        class ViewHolder {
            TextView sfc_mannolz_list_prosscode;
            TextView sfc_mannolz_list_prossname;
            TextView sfc_mannolz_list_manqty;
            TextView sfc_mannolz_list_issqty;
            TextView sfc_mannolz_list_areaname;
            TextView sfc_mannolz_list_nextpross;
            TextView sfc_mannolz_list_nextprossname;
            TextView sfc_mannolz_list_date;
            TextView sfc_mannolz_list_user;
        }
    }
    class  GdlzInfoBean{
        private String Manno;
        private int Manqty;
        private int Issqty;
        private String ProssCode;
        private String ProssName;
        private String NextPross;
        private String NextProssName;
        private String AreaId;
        private String AreaName;
        private String UserNo;
        private String UserName;
        private String CreateTime;

        public String getManno() {
            return Manno;
        }

        public void setManno(String manno) {
            Manno = manno;
        }

        public int getManqty() {
            return Manqty;
        }

        public void setManqty(int manqty) {
            Manqty = manqty;
        }

        public int getIssqty() {
            return Issqty;
        }

        public void setIssqty(int issqty) {
            Issqty = issqty;
        }

        public String getProssCode() {
            return ProssCode;
        }

        public void setProssCode(String prossCode) {
            ProssCode = prossCode;
        }

        public String getProssName() {
            return ProssName;
        }

        public void setProssName(String prossName) {
            ProssName = prossName;
        }

        public String getNextPross() {
            return NextPross;
        }

        public void setNextPross(String nextPross) {
            NextPross = nextPross;
        }

        public String getNextProssName() {
            return NextProssName;
        }

        public void setNextProssName(String nextProssname) {
            NextProssName = nextProssname;
        }

        public String getAreaId() {
            return AreaId;
        }

        public void setAreaId(String areaId) {
            AreaId = areaId;
        }

        public String getAreaName() {
            return AreaName;
        }

        public void setAreaName(String areaName) {
            AreaName = areaName;
        }

        public String getUserNo() {
            return UserNo;
        }

        public void setUserNo(String userNo) {
            UserNo = userNo;
        }

        public String getUserName() {
            return UserName;
        }

        public void setUserName(String userName) {
            UserName = userName;
        }

        public String getCreateTime() {
            return CreateTime;
        }

        public void setCreateTime(String createTime) {
            CreateTime = createTime;
        }
    }
}
